package com.tinem.platform.service.message.api;

import com.baidu.fsg.uid.UidGenerator;
import com.tinem.platform.module.pojo.vo.message.PushReq;
import com.tinem.platform.module.pojo.vo.message.PushRes;
import com.tinem.platform.module.starter.web.context.GatewayContext;
import com.tinem.platform.service.message.observable.MessageObservable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className MessageApi
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-08 18:46
 */
@Slf4j
@RestController
@Api(value = "push")
public class PushApi {

    @Resource
    private UidGenerator uidGenerator;
    @Autowired
    private ApplicationContext applicationContext;

    @PostMapping("/push.api")
    @ApiOperation(value = "消息推送服务", httpMethod = "POST", notes = "暂无")
    public PushRes push(@RequestBody PushReq pushReq, HttpServletRequest request){
        MessageObservable messageObservable = new MessageObservable(pushReq,GatewayContext.getClient(),uidGenerator.getUID(),pushReq.getBody());
        applicationContext.publishEvent(messageObservable);
        return new PushRes(messageObservable.getId(),messageObservable.getLinks());
    }
}
